
#include "3dTrig.h"
#include "MatrixConvert.h"
#include "TrigMath.h"


C3dTrig::C3dTrig()
{
	float init[] = {0,0,100,1};
	poly1_pos = init;

	init[0] = 0;
	init[1] = 0;
	init[2] = -100;
	init[3] = 1;
	cam_pos		= init;

	init[0] = 0;
	init[1] = 0;
	init[2] = 0;
	init[3] = 1;
	cam_dir		= init;
}
	
C3dTrig::~C3dTrig()
{

}

int C3dTrig::Game_Init()
{
	// this function is where you do all the initialization 
	// for your game

	int index; // looping var

	// start up DirectDraw (replace the parms as you desire)
	int nRet = m_ddraw.Init(m_hWnd, m_WndWidth, m_WndHeight, m_bpp, m_bFullScreen);
	//if (nRet != 0)
	//	return-1;

	m_graphGdi.Init(&m_ddraw);

	// initialize directinput
	m_gameInput.Init(m_hInstance, m_hWnd);

	// acquire the keyboard 
	m_gameInput.Init_Keyboard();


	// initialize directsound and directmusic
	m_gameSound.Init(m_hWnd);
	m_gameMusic.Init(m_hWnd, m_gameSound.GetDSound());

	// hide the mouse
	ShowCursor(FALSE);

	// seed random number generator
	srand(m_gameClock.Start_Clock());

	//
	CTrigMath::Build_Sin_Cos_Tables();

	// initialize a single polygon
	poly1.state  = POLY4DV1_STATE_ACTIVE;
	poly1.attr   =  0; 
	poly1.color  = RGB16Bit(0,255,0);

	poly1.vlist[0].SetX(0);
	poly1.vlist[0].SetY(50);
	poly1.vlist[0].SetZ(0);
	poly1.vlist[0].SetW(1);

	poly1.vlist[1].SetX(50);
	poly1.vlist[1].SetY(-50);
	poly1.vlist[1].SetZ(0);
	poly1.vlist[1].SetW(1);

	poly1.vlist[2].SetX(-50);
	poly1.vlist[2].SetY(-50);
	poly1.vlist[2].SetZ(0);
	poly1.vlist[2].SetW(1);;

	poly1.next = poly1.prev = NULL;

	// initialize the camera with 90 FOV, normalized coordinates
	CPoint4d target;
	cam.Init(
		CAM_MODEL_EULER, // euler camera model
		cam_pos,  // initial camera position
		cam_dir,  // initial camera angles
		target,      // no initial target
		50.0,      // near and far clipping planes
		500.0,
		90.0,      // field of view in degrees
		m_WndWidth,   // size of final screen viewport
		m_WndHeight);

	// return success
	return 0;

} // end Game_Init


int	C3dTrig::Game_Main()
{
	// this is the workhorse of your game it will be called
	// continuously in real-time this is like main() in C
	// all the calls for you game go here!

	static CMatrix44 mrot; // general rotation matrix
	static float ang_y = 0;      // rotation angle

	int index; // looping var

	// start the timing clock
	m_gameClock.Start_Clock();

	// clear the drawing surface 
	m_ddraw.Fill_Surface(BACK_SURFACE, 0);

	// read keyboard and other devices here
	m_gameInput.Init_Keyboard();

	// game logic here...

	// initialize the renderlist
	rend_list.Reset();
	
	// lock the back buffer
	m_ddraw.Lock_Back_Surface();

	RECT clipRect = {0, 0, m_WndWidth, m_WndHeight};
	rend_list.Init(m_ddraw.GetBackBuffer(), m_ddraw.GetBackLpitch(), clipRect, &cam);

	// insert polygon into the renderlist
	rend_list.Insert_POLYF4DV1(&poly1);

	// generate rotation matrix around y axis
	XYZ_Rotation_MATRIX4X4(0, ang_y, 0, mrot);

	// rotate polygon slowly
	if (++ang_y >= 360.0) ang_y = 0;

	// rotate the local coords of single polygon in renderlist
	rend_list.Transform(mrot, TRANSFORM_LOCAL_ONLY);

	// perform local/model to world transform
	rend_list.Model_To_World(poly1_pos);

	// generate camera matrix
	cam.Build_Matrix_Euler(CAM_ROT_SEQ_ZYX);

	// apply world to camera transform
	rend_list.World_To_Camera();

	// apply camera to perspective transformation
	rend_list.Camera_To_Perspective();

	// apply screen transform
	rend_list.Perspective_To_Screen();

	// draw instructions
	m_graphGdi.Draw_Text("Press ESC to exit.",0,0,RGB(0,255,0));


	// render the polygon list
	rend_list.Draw_Wire16();

	// unlock the back buffer
	m_ddraw.Unlock_Back_Surface();

	// flip the surfaces
	m_ddraw.Flip(m_Window_client_x0, m_Window_client_y0);

	// sync to 30ish fps
	m_gameClock.Wait_Clock(30);

	// check of user is trying to exit
	if (KEY_DOWN(VK_ESCAPE) || m_gameInput.GetKeyState(DIK_ESCAPE))
	{
		PostMessage(m_hWnd, WM_DESTROY,0,0);

	} // end if

	return 0;
}

int	C3dTrig::Game_Shutdown()
{
	// this function is where you shutdown your game and
	// release all resources that you allocated

	// shut everything down

	// release all your resources created for the game here....


	CGameMain::Game_Shutdown();

	return 0;
}
